package com.example.agriapp.util;

import android.content.Context;
import android.content.SharedPreferences;

public class SharedPrefUtils {
    private static final String SP_NAME = "agriculture_app_prefs";
    private static SharedPreferences sp;

    public static SharedPreferences getSharedPreferences(Context context) {
        if (sp == null) {
            sp = context.getSharedPreferences(SP_NAME, Context.MODE_PRIVATE);
        }
        return sp;
    }

    public static void putInt(Context context, String key, int value) {
        SharedPreferences.Editor editor = getSharedPreferences(context).edit();
        editor.putInt(key, value);
        editor.apply();
    }

    public static int getInt(Context context, String key, int defaultValue) {
        return getSharedPreferences(context).getInt(key, defaultValue);
    }

    public static void putString(Context context, String key, String value) {
        SharedPreferences.Editor editor = getSharedPreferences(context).edit();
        editor.putString(key, value);
        editor.apply();
    }

    public static String getString(Context context, String key, String defaultValue) {
        return getSharedPreferences(context).getString(key, defaultValue);
    }

    public static void putBoolean(Context context, String key, boolean value) {
        SharedPreferences.Editor editor = getSharedPreferences(context).edit();
        editor.putBoolean(key, value);
        editor.apply();
    }

    public static boolean getBoolean(Context context, String key, boolean defaultValue) {
        return getSharedPreferences(context).getBoolean(key, defaultValue);
    }

    public static void remove(Context context, String key) {
        SharedPreferences.Editor editor = getSharedPreferences(context).edit();
        editor.remove(key);
        editor.apply();
    }

    public static void clear(Context context) {
        SharedPreferences.Editor editor = getSharedPreferences(context).edit();
        editor.clear();
        editor.apply();
    }

    // 用户相关快捷方法
    public static void saveUserInfo(Context context, int userId, String userName, String userType, String avatar) {
        SharedPreferences.Editor editor = getSharedPreferences(context).edit();
        editor.putInt(Constants.SP_KEY_USER_ID, userId);
        editor.putString(Constants.SP_KEY_USER_NAME, userName);
        editor.putString(Constants.SP_KEY_USER_TYPE, userType);
        editor.putString(Constants.SP_KEY_USER_AVATAR, avatar);
        editor.putBoolean(Constants.SP_KEY_LOGIN_STATUS, true);
        editor.apply();
    }

    public static void clearUserInfo(Context context) {
        remove(context, Constants.SP_KEY_USER_ID);
        remove(context, Constants.SP_KEY_USER_NAME);
        remove(context, Constants.SP_KEY_USER_TYPE);
        remove(context, Constants.SP_KEY_USER_AVATAR);
        putBoolean(context, Constants.SP_KEY_LOGIN_STATUS, false);
    }

    public static boolean isLoggedIn(Context context) {
        return getBoolean(context, Constants.SP_KEY_LOGIN_STATUS, false);
    }

    public static String getUserType(Context context) {
        return getString(context, Constants.SP_KEY_USER_TYPE, "");
    }

    public static int getUserId(Context context) {
        return getInt(context, Constants.SP_KEY_USER_ID, -1);
    }

    public static String getUserName(Context context) {
        return getString(context, Constants.SP_KEY_USER_NAME, "");
    }

    public static String getUserAvatar(Context context) {
        return getString(context, Constants.SP_KEY_USER_AVATAR, Constants.DEFAULT_AVATAR);
    }
}